﻿using Panuon.UI.Silver;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;

namespace Dt261.converter
{
    public class EnumToBooleanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value == null ? false : value.Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value != null && value.Equals(true) ? parameter : Binding.DoNothing;
        }
    }

    public class MultiStringStateConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            if (values == null) return null;

            if (values[0] == null) return null;

            if (values[1] == null) return null;

            double value1, value2;

            if(Double.TryParse(values[0].ToString(), out value1) && Double.TryParse(values[1].ToString(), out value2))
            {
                return value1 - value2;
            }


            //if (parameter == null) return string.Empty;

            //if (values.Length != 2) return null;

            //if (values[1] != null && values[1].ToString() == "###") return null;

            //var ps = parameter.ToString().Split('-');

            //if (ps.Length != 2) return null;



            //if (values[0].ToString().ToDouble() < ps[0].ToDouble()) return "↓";

            //if (values[0].ToString().ToDouble() > ps[1].ToDouble()) return "↑";

            return string.Empty;
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
 

    public class DPEPConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && int.TryParse(value.ToString(), out int result))
            {
                if (result.Equals(2))
                    return "DP";
                else
                    return "TE";
            }
            return "DP"; ;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && bool.TryParse(value.ToString(), out bool result))
            {
                if (result.Equals("DP"))
                    return 2;
                else
                    return 1;
            }
            return 2;
        }
    }
    public class SexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && int.TryParse(value.ToString(), out int result))
            {
                if (result.Equals(0))
                    return "男";
                else
                    return "女";
            }
            return "女"; ;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && bool.TryParse(value.ToString(), out bool result))
            {
                if (result.Equals("男"))
                    return 0;
                else
                    return 1;
            }
            return 1;
        }
    }

    public class AgeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                DateTime a = (DateTime)value;

                int age = System.Convert.ToInt32(DateTime.Now.Year) - System.Convert.ToInt32(a.Year);
                if (age > 0)
                    return age.ToString() + " 岁";
                else
                    return "未知年龄";
            }
            return "未知年龄"; ;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {

            return value;
        }
    }

    public class TestTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && int.TryParse(value.ToString(), out int result))
            {
                if (result.Equals(1))
                    return "纯音骨导";
                else
                    return "纯音气导";
            }
            return "纯音气导";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && bool.TryParse(value.ToString(), out bool result))
            {
                if (result.Equals("纯音骨导"))
                    return 1;
                else
                    return 0;
            }
            return 0;
        }
    }

    public class TestTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                DateTime t = new DateTime();

                if (t.Equals(value))
                    return " - 无";
                return value;
            }
            return "无";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class RadioButtonSexConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                if (value.Equals(true) && 1 == int.Parse(parameter.ToString()))
                    return true;
                else if (value.Equals(false) && 0 == int.Parse(parameter.ToString()))
                    return true;
                else
                    return false;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return (value.Equals(true) && 1 == int.Parse(parameter.ToString()) || value.Equals(false) && 0 == int.Parse(parameter.ToString())) ? true : false;
        }
    }


    public class RadioButtonConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                if (value.Equals(2) && 2 == int.Parse(parameter.ToString()))
                    return true;//双图
                else if (value.Equals(1) && 1 == int.Parse(parameter.ToString()))
                    return true;//单图，
                else if (value.Equals(0) && 0 == int.Parse(parameter.ToString()))
                    return true;//无图
                else
                    return false;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return parameter;
        }
    }

    public class SpeechTestTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {

                System.DateTime startTime = TimeZone.CurrentTimeZone.ToLocalTime(new System.DateTime(1970, 1, 1));//当地时区
                var time = startTime.AddSeconds(Double.Parse(value.ToString()) / 1000);


                return time;
            }
            return "";
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return value;
        }
    }

    public class SpeechNumberConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && int.TryParse(value.ToString(), out int result))
            {
                if (result.Equals(0))
                    return "双耳";
                else
                    if (result.Equals(1))
                    return "左耳";
                else
                    return "右耳"; ;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && bool.TryParse(value.ToString(), out bool result))
            {
                if (result)
                    return 1;
                else
                    return 0;
            }
            return false;
        }
    }

    public class NumberConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && int.TryParse(value.ToString(), out int result))
            {
                if (result.Equals(1))
                    return true;
                else
                    return false;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && bool.TryParse(value.ToString(), out bool result))
            {
                if (result)
                    return 1;
                else
                    return 0;
            }
            return false;
        }
    }

    public class BcRightMarkTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && int.TryParse(value.ToString(), out int result))
            {
                if (result.Equals(13) && 13 == int.Parse(parameter.ToString()))

                    return true;
                else
                     if (result.Equals(10) && 10 == int.Parse(parameter.ToString()))

                    return true;
                else
                     if (result.Equals(3) && 3 == int.Parse(parameter.ToString()))

                    return true;
                else
                     if (result.Equals(4) && 4 == int.Parse(parameter.ToString()))

                    return true;
                return false;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //if (value != null && bool.TryParse(value.ToString(), out bool result))
            //{
            //    if (result)
            //        return 13;
            //    else
            //        return 10;
            //}
            return parameter;
        }
    }

    public class BcMaskRightMarkTypeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null && int.TryParse(value.ToString(), out int result))
            {
                if (result.Equals(7) && 7 == int.Parse(parameter.ToString()))

                    return true;
                else
                if (result.Equals(5) && 5 == int.Parse(parameter.ToString()))

                    return true;
                return false;
            }
            return false;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            //if (value != null && bool.TryParse(value.ToString(), out bool result))
            //{
            //    if (result)
            //        return 5;
            //    else
            //        return 7;
            //}
            return parameter;
        }
    }

    /// <summary>
    /// 自动列表序号
    /// </summary>
    [ValueConversion(typeof(Int32), typeof(ListViewItem))]
    public class IndexConverter : IValueConverter
    {
        public object Convert(object value, Type TargetType, object parameter, CultureInfo culture)
        {
            ListViewItem item = (ListViewItem)value;
            ListView listView = ItemsControl.ItemsControlFromItemContainer(item) as ListView;
            return listView.ItemContainerGenerator.IndexFromContainer(item) + 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
